package com.quanyan.runningriding.util;

import com.quanyan.runningriding.entity.vo.run.*;
import com.quanyan.runningriding.entity.vo.run.resp.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * CommonUtil
 *
 * @author Tu Chun
 * @date 2016/8/23
 */
public class CommonUtil {

    public static double keepTwoDecimal(double f) {
        BigDecimal b = new BigDecimal(f);
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    public static String getLatLngStr(List<LatLngInfo> latLngInfos) {
        StringBuilder stringBuilder = new StringBuilder();
        if (latLngInfos != null && latLngInfos.size() > 0) {
            for (int i = 0; i < latLngInfos.size(); i++) {
                LatLngInfo lngInfo = latLngInfos.get(i);
                if (i == latLngInfos.size() - 1) {
                    stringBuilder.append((lngInfo.getLat() != null ? lngInfo.getLat() : 0.00) + "," + (lngInfo.getLng() != null ? lngInfo.getLng() : 0.00) + "," + (lngInfo.getTime() != null ? lngInfo.getTime() : 0));
                } else {
                    stringBuilder.append((lngInfo.getLat() != null ? lngInfo.getLat() : 0.00) + "," + (lngInfo.getLng() != null ? lngInfo.getLng() : 0.00) + "," + (lngInfo.getTime() != null ? lngInfo.getTime() : 0) + ";");
                }

            }
        }

        return stringBuilder.toString();
    }

    public static String getPerKiloPaceInfoStr(List<PerkiloPaceInfo> perkiloPaceInfos) {
        StringBuilder stringBuilder = new StringBuilder();
        if (perkiloPaceInfos != null && perkiloPaceInfos.size() > 0) {
            for (int i = 0; i < perkiloPaceInfos.size(); i++) {
                PerkiloPaceInfo paceInfo = perkiloPaceInfos.get(i);
                if (i == perkiloPaceInfos.size() - 1) {
                    stringBuilder.append(paceInfo.getKiloNum() + "," + (paceInfo.getPace() != null ? paceInfo.getPace() : 0) + "," + paceInfo.getTime());
                } else {
                    stringBuilder.append(paceInfo.getKiloNum() + "," + (paceInfo.getPace() != null ? paceInfo.getPace() : 0) + "," + paceInfo.getTime() + ";");
                }
            }
        }

        return stringBuilder.toString();
    }

    public static String getPermeterStrideInfoStr(List<PermeterStrideInfo> permeterStrideInfos) {
        StringBuilder stringBuilder = new StringBuilder();
        if (permeterStrideInfos != null && permeterStrideInfos.size() > 0) {
            for (int i = 0; i < permeterStrideInfos.size(); i++) {
                PermeterStrideInfo strideInfo = permeterStrideInfos.get(i);
                if (i == permeterStrideInfos.size() - 1) {
                    stringBuilder.append(strideInfo.getCoordinateX() +","+ (strideInfo.getStride() != null ? strideInfo.getStride() : 0) + "," + strideInfo.getTime());
                } else {
                    stringBuilder.append(strideInfo.getCoordinateX() +","+ (strideInfo.getStride() != null ? strideInfo.getStride() : 0) + "," + strideInfo.getTime() + ";");
                }
            }
        }

        return stringBuilder.toString();
    }

    public static String getPermeterAltitudeInfoStr(List<PermeterAltitudeInfo> permeterAltitudeInfos) {
        StringBuilder stringBuilder = new StringBuilder();
        if (permeterAltitudeInfos != null && permeterAltitudeInfos.size() > 0) {
            for (int i = 0; i < permeterAltitudeInfos.size(); i++) {
                PermeterAltitudeInfo altitudeInfo = permeterAltitudeInfos.get(i);
                if (i == permeterAltitudeInfos.size() - 1) {
                    stringBuilder.append(altitudeInfo.getCoordinateX() +","+ (altitudeInfo.getAltitude() != null ? altitudeInfo.getAltitude() : 0) + "," + altitudeInfo.getTime());
                } else {
                    stringBuilder.append(altitudeInfo.getCoordinateX() +","+ (altitudeInfo.getAltitude() != null ? altitudeInfo.getAltitude() : 0) + "," + altitudeInfo.getTime() + ";");
                }
            }
        }

        return stringBuilder.toString();
    }

    public static String getPermeterCadenceInfoStr(List<PermeterCadenceInfo> permeterCadenceInfos) {
        StringBuilder stringBuilder = new StringBuilder();
        if (permeterCadenceInfos != null && permeterCadenceInfos.size() > 0) {
            for (int i = 0; i < permeterCadenceInfos.size(); i++) {
                PermeterCadenceInfo cadenceInfo = permeterCadenceInfos.get(i);
                if (i == permeterCadenceInfos.size() - 1) {
                    stringBuilder.append(cadenceInfo.getCoordinateX() +","+ (cadenceInfo.getCadence() != null ? cadenceInfo.getCadence() : 0) + "," + cadenceInfo.getTime());
                } else {
                    stringBuilder.append(cadenceInfo.getCoordinateX() +","+ (cadenceInfo.getCadence() != null ? cadenceInfo.getCadence() : 0) + "," + cadenceInfo.getTime() + ";");
                }
            }
        }

        return stringBuilder.toString();
    }

    public static RunTrackDetail getRunTrackDetail(RunTrackDetailMap trackDetailMap) {
        RunTrackDetail runTrackDetail = new RunTrackDetail();
        if (trackDetailMap != null) {
            runTrackDetail.setId(trackDetailMap.getId());
            runTrackDetail.setRunName(trackDetailMap.getRunName());
            runTrackDetail.setAvgPace(trackDetailMap.getAvgPace());
            runTrackDetail.setDistance(trackDetailMap.getDistance());
            runTrackDetail.setStartTime(trackDetailMap.getStartTime().getTime());
            runTrackDetail.setEndTime(trackDetailMap.getEndTime().getTime());
            runTrackDetail.setExerciseTime(trackDetailMap.getExerciseTime());
            String latLngStrs = trackDetailMap.getLatLngs();
            String[] latLngArrs = latLngStrs.split(";");
            List<LatLngInfo> lngInfos = new ArrayList<LatLngInfo>();
            if (latLngArrs != null && latLngArrs.length != 0) {
                for (int i = 0; i < latLngArrs.length; i++) {
                    LatLngInfo lngInfo = new LatLngInfo();
                    String latLngStr = latLngArrs[i];
                    String[] latLngArr = latLngStr.split(",");
                    for (int j = 0; j < latLngArr.length; j++) {
                        if (j == 0)
                            lngInfo.setLat((latLngArr[0] != null && !"".equals(latLngArr[0])) ? Double.parseDouble(latLngArr[0]) : 0.00);
                        if (j == 1)
                            lngInfo.setLng((latLngArr[1] != null && !"".equals(latLngArr[1])) ? Double.parseDouble(latLngArr[1]) : 0.00);
                        if (j == 2)
                            lngInfo.setTime((latLngArr[2] != null && !"".equals(latLngArr[2])) ? Long.parseLong(latLngArr[2]) : 0);
                    }
                    lngInfos.add(lngInfo);
                }
            }
            runTrackDetail.setLatLngInfos(lngInfos);
        }
        return runTrackDetail;
    }
    public static ChartDetail getchartDetail(ChartDetailMap chartDetailMap) {
        ChartDetail chartDetail = new ChartDetail();
        if (chartDetailMap != null) {
            chartDetail.setId(chartDetailMap.getId());
            chartDetail.setMaxPace(chartDetailMap.getMaxPace());
            chartDetail.setMinPace(chartDetailMap.getMinPace());
            chartDetail.setAvgPace(chartDetailMap.getAvgPace());
            //获取配速
            String kiloPaceStrs = chartDetailMap.getPerkiloPaceInfoStr();
            String[] kiloPaceArrs = kiloPaceStrs.split(";");
            List<PerkiloPaceInfo> paceInfos = new ArrayList<PerkiloPaceInfo>();
            if (kiloPaceArrs != null && kiloPaceArrs.length != 0) {
                for (int i = 0; i < kiloPaceArrs.length; i++) {
                    PerkiloPaceInfo paceInfo = new PerkiloPaceInfo();
                    String kiloPaceStr = kiloPaceArrs[i];
                    String[] latLngArr = kiloPaceStr.split(",");
                    for (int j = 0; j < latLngArr.length; j++) {
                        if (j == 0)
                            paceInfo.setKiloNum((latLngArr[0] != null && !"".equals(latLngArr[0])) ? Integer.parseInt(latLngArr[0]) : 0);
                        if (j == 1)
                            paceInfo.setPace((latLngArr[1] != null && !"".equals(latLngArr[1])) ? Long.parseLong(latLngArr[1]) : 0);
                        if (j == 2)
                            paceInfo.setTime((latLngArr[2] != null && !"".equals(latLngArr[2])) ? Long.parseLong(latLngArr[2]) : 0);
                    }

                    paceInfos.add(paceInfo);
                }
            }
            chartDetail.setPerkiloPaceInfos(paceInfos);
            //获取海拔
            String altitudeInfoStrs = chartDetailMap.getPermeterAltitudeInfoStr();
            String[] altitudeArrs = altitudeInfoStrs.split(";");
            List<PermeterAltitudeInfo> permeterAltitudeInfos = new ArrayList<PermeterAltitudeInfo>();
            if (altitudeArrs != null && altitudeArrs.length != 0) {
                for (int i = 0; i < altitudeArrs.length; i++) {
                    PermeterAltitudeInfo permeterAltitudeInfo = new PermeterAltitudeInfo();
                    String altitudeArr = altitudeArrs[i];
                    String[] altitudeArrss = altitudeArr.split(",");
                    for (int j = 0; j < altitudeArrss.length; j++) {
                        if (j == 0)
                            permeterAltitudeInfo.setCoordinateX((altitudeArrss[0] != null && !"".equals(altitudeArrss[0])) ? Integer.parseInt(altitudeArrss[0]) : 0);
                        if (j == 1)
                            permeterAltitudeInfo.setAltitude((altitudeArrss[1] != null && !"".equals(altitudeArrss[1])) ? Double.parseDouble(altitudeArrss[1]) : 0);
                        if (j == 2)
                            permeterAltitudeInfo.setTime((altitudeArrss[2] != null && !"".equals(altitudeArrss[2])) ? Long.parseLong(altitudeArrss[2]) : 0);
                    }

                    permeterAltitudeInfos.add(permeterAltitudeInfo);
                }
            }
            chartDetail.setPermeterAltitudeInfos(permeterAltitudeInfos);
           //获取步频
            String cadenceInfoStrs = chartDetailMap.getPermeterCadenceInfoStr();
            String[] cadenceInfoArrs = cadenceInfoStrs.split(";");
            List<PermeterCadenceInfo> permeterCadenceInfos = new ArrayList<PermeterCadenceInfo>();
            if (cadenceInfoArrs != null && cadenceInfoArrs.length != 0) {
                for (int i = 0; i < cadenceInfoArrs.length; i++) {
                    PermeterCadenceInfo permeterCadenceInfo = new PermeterCadenceInfo();
                    String cadenceInfoArr = cadenceInfoArrs[i];
                    String[] cadenceInfoArrss = cadenceInfoArr.split(",");
                    for (int j = 0; j < cadenceInfoArrss.length; j++) {
                        if (j == 0)
                            permeterCadenceInfo.setCoordinateX((cadenceInfoArrss[0] != null && !"".equals(cadenceInfoArrss[0])) ? Integer.parseInt(cadenceInfoArrss[0]) : 0);
                        if (j == 1)
                            permeterCadenceInfo.setCadence((cadenceInfoArrss[1] != null && !"".equals(cadenceInfoArrss[1])) ? Integer.parseInt(cadenceInfoArrss[1]) : 0);
                        if (j == 2)
                            permeterCadenceInfo.setTime((cadenceInfoArrss[2] != null && !"".equals(cadenceInfoArrss[2])) ? Long.parseLong(cadenceInfoArrss[2]) : 0);
                    }

                    permeterCadenceInfos.add(permeterCadenceInfo);
                }
            }
            chartDetail.setPermeterCadenceInfos(permeterCadenceInfos);
            //获取步幅
            String strideInfoStrs = chartDetailMap.getPermeterStrideInfoStr();
            String[] strideInfoArrs = strideInfoStrs.split(";");
            List<PermeterStrideInfo> permeterStrideInfos = new ArrayList<PermeterStrideInfo>();
            if (strideInfoArrs != null && strideInfoArrs.length != 0) {
                for (int i = 0; i < strideInfoArrs.length; i++) {
                    PermeterStrideInfo permeterStrideInfo = new PermeterStrideInfo();
                    String strideInfoArr = strideInfoArrs[i];
                    String[] strideInfoArrss = strideInfoArr.split(",");
                    for (int j = 0; j < strideInfoArrss.length; j++) {
                        if (j == 0)
                            permeterStrideInfo.setCoordinateX((strideInfoArrss[0] != null && !"".equals(strideInfoArrss[0])) ? Integer.parseInt(strideInfoArrss[0]) : 0);
                        if (j == 1)
                            permeterStrideInfo.setStride((strideInfoArrss[1] != null && !"".equals(strideInfoArrss[1])) ? Integer.parseInt(strideInfoArrss[1]) : 0);
                        if (j == 2)
                            permeterStrideInfo.setTime((strideInfoArrss[2] != null && !"".equals(strideInfoArrss[2])) ? Long.parseLong(strideInfoArrss[2]) : 0);
                    }

                    permeterStrideInfos.add(permeterStrideInfo);
                }
            }
            chartDetail.setPermeterStrideInfos(permeterStrideInfos);

        }
        return chartDetail;
    }

    public static RunRecordDetail getRunTrackDetail(RunRecordDetailMap recordDetailMap) {
        RunRecordDetail runRecordDetail = new RunRecordDetail();
        if (recordDetailMap != null) {
            runRecordDetail.setId(recordDetailMap.getId());
            runRecordDetail.setRunName(recordDetailMap.getRunName());
            runRecordDetail.setMaxPace(recordDetailMap.getMaxPace());
            runRecordDetail.setMinPace(recordDetailMap.getMinPace());
            runRecordDetail.setAvgPace(recordDetailMap.getAvgPace());
            runRecordDetail.setCadence(recordDetailMap.getCadence());
            runRecordDetail.setCalories(recordDetailMap.getCalories());
            runRecordDetail.setStepCount(recordDetailMap.getStepCount());
            runRecordDetail.setDistance(recordDetailMap.getDistance());
            runRecordDetail.setStride(recordDetailMap.getStride());
            runRecordDetail.setAltitude(recordDetailMap.getAltitude());
            runRecordDetail.setCumulativeClimb(recordDetailMap.getCumulativeClimb());
            runRecordDetail.setStartTime(recordDetailMap.getStartTime().getTime());
            runRecordDetail.setEndTime(recordDetailMap.getEndTime().getTime());
            runRecordDetail.setExerciseTime(recordDetailMap.getExerciseTime());
            String kiloPaceStrs = recordDetailMap.getPerkiloPaceInfoStr();
            String[] kiloPaceArrs = kiloPaceStrs.split(";");
            List<PerkiloPaceInfo> paceInfos = new ArrayList<PerkiloPaceInfo>();
            if (kiloPaceArrs != null && kiloPaceArrs.length != 0) {
                for (int i = 0; i < kiloPaceArrs.length; i++) {
                    PerkiloPaceInfo paceInfo = new PerkiloPaceInfo();
                    String kiloPaceStr = kiloPaceArrs[i];
                    String[] latLngArr = kiloPaceStr.split(",");
                    for (int j = 0; j < latLngArr.length; j++) {
                        if (j == 0)
                            paceInfo.setKiloNum((latLngArr[0] != null && !"".equals(latLngArr[0])) ? Integer.parseInt(latLngArr[0]) : 0);
                        if (j == 1)
                            paceInfo.setPace((latLngArr[1] != null && !"".equals(latLngArr[1])) ? Long.parseLong(latLngArr[1]) : 0);
                        if (j == 2)
                            paceInfo.setTime((latLngArr[2] != null && !"".equals(latLngArr[2])) ? Long.parseLong(latLngArr[2]) : 0);
                    }

                    paceInfos.add(paceInfo);
                }
            }
            runRecordDetail.setPerkiloPaceInfos(paceInfos);
        }
        return runRecordDetail;
    }


}
